package me.magicall.myblog.util;

import java.util.Arrays;
import java.util.List;

import me.magicall.coll.CollFactory;
import me.magicall.coll.ElementTransformer;
import me.magicall.util.Utils;

public class U implements MyBlogConst {

	public static boolean compareJavaNameAndDbName(final String javaName, final String dbName) {
		final int len2 = dbName.length();
		if (javaName.length() > len2) {
			return false;
		}
		for (int i = 0; i < len2; ++i) {
			final char c2 = dbName.charAt(i);
			if (c2 == '_') {
				continue;
			}
			if (!Utils.CHAR.equalsIgnoreCase(javaName.charAt(i), c2)) {
				return false;
			}
		}
		return true;
	}

	public static List<? extends Number> parseToRangeNumber(final String idStr, final int maxSize) {
		if (idStr.contains("~") || idStr.contains("-")) {//1~10
			final String[] idStrs = idStr.split("~|-");
			final String startIdStr = idStrs[0];
			final String endIdStr = idStrs[1];
//			if (idIsInt) {
//				final int startId = Utils.INT.fromString(startIdStr);
//				final int endId = Utils.INT.fromString(endIdStr) + 1;
//				return CollFactory.L.seq(startId, endId - startId);
//			} else {
			final long startId = Utils.LONG.fromString(startIdStr);
			final long endId = Utils.LONG.fromString(endIdStr);
			final int size = (int) (endId - startId) + 1;
			return CollFactory.L.seq(startId, Math.min(size, maxSize));
//			}
		}
		return Utils.LIST.emptyValue();
	}

	public static List<? extends Number> parseNumbers(final String idStr, final int maxSize) {
		List<? extends Number> ids;
		if (idStr.contains("~")) {//1~10
			ids = parseToRangeNumber(idStr, maxSize);
		} else {//1,2,3,4
			final String[] idStrs = idStr.split(",");
			ids = Utils.LIST.head(Utils.LIST.transform(Arrays.asList(idStrs), new ElementTransformer<String, Number>() {
				@Override
				public Number transform(final int index, final String element) {
					//					if (idIsInt) {
					//						return Utils.INT.fromString(element);
					//					} else {
					return Utils.LONG.fromString(element);
					//					}
				}
			}), maxSize);

		}
		return ids;
	}
}
